package com.ruoyi.system.controller;

import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.reservationserver.service.DynamicService;
import com.ruoyi.reservationserver.utils.Result;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Dynamic;
import com.ruoyi.system.service.IDynamicService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 动态文章管理Controller
 * 
 * @author ruoyi
 * @date 2023-08-12
 */
@RestController
@RequestMapping("/system/dynamic")
public class DynamicController extends BaseController
{
    @Autowired
    private IDynamicService dynamicService;
    /**
     * 服务对象
     */
    @Resource
    private DynamicService dynamicService1;


    /**
     * 获取动态新闻列表
     * @return
     */
    @GetMapping("/getDynamic")
    public Result getDynamic(){

        List<com.ruoyi.reservationserver.entity.Dynamic> dynamics = dynamicService1.list();
        if (dynamics.isEmpty()){
            return Result.error("不存在新聞");
        }else {
            return Result.success(dynamics);
        }
    }

    /**
     * 获取动态新闻列表数量
     * @return
     */
    @GetMapping("/getDynamicNum")
    public Result getDynamicNum(){
        Result result = new Result();
        int count = dynamicService1.count();
        if (count <0){
            return Result.error("500","错误");
        }else {
            return Result.success(count);
        }
    }


    /**
     * 新增数据
     *
     * @param dynamic 实体
     * @return 新增结果
     */
    @PostMapping("add")
    public Result add(@RequestBody com.ruoyi.reservationserver.entity.Dynamic dynamic) {
        if (dynamic==null){
            return Result.error("数据不能为空。");
        }
        return Result.success(this.dynamicService1.insert(dynamic));
    }

    /**
     * 编辑数据
     *
     * @param dynamic 实体
     * @return 编辑结果
     */
    @PostMapping("/edit")
    public Result edit(@RequestBody com.ruoyi.reservationserver.entity.Dynamic dynamic) {
        System.out.println(dynamic);
        if (dynamic==null){
            return Result.error("返回数据不能为空");
        }
        this.dynamicService1.update(dynamic);
        return Result.success();
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @GetMapping("/del")
    public Result deleteById(@RequestParam("dynamicId") Integer id) {

        try {

            if(this.dynamicService1.deleteById(id)){
                return Result.success();
            }else {
                return Result.error("删除失败");
            }

        }catch (Exception e) {
            System.out.println(e);
            return Result.error("删除异常");
        }

    }


    /**
     * 查询动态文章管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:dynamic:list')")
    @GetMapping("/list")
    public TableDataInfo list(Dynamic dynamic)
    {
        startPage();
        List<Dynamic> list = dynamicService.selectDynamicList(dynamic);
        return getDataTable(list);
    }

    /**
     * 导出动态文章管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:dynamic:export')")
    @Log(title = "动态文章管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Dynamic dynamic)
    {
        List<Dynamic> list = dynamicService.selectDynamicList(dynamic);
        ExcelUtil<Dynamic> util = new ExcelUtil<Dynamic>(Dynamic.class);
        util.exportExcel(response, list, "动态文章管理数据");
    }

    /**
     * 获取动态文章管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:dynamic:query')")
    @GetMapping(value = "/{dynamicId}")
    public AjaxResult getInfo(@PathVariable("dynamicId") Long dynamicId)
    {
        return success(dynamicService.selectDynamicByDynamicId(dynamicId));
    }

    /**
     * 新增动态文章管理
     */
    @PreAuthorize("@ss.hasPermi('system:dynamic:add')")
    @Log(title = "动态文章管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Dynamic dynamic)
    {
        return toAjax(dynamicService.insertDynamic(dynamic));
    }

    /**
     * 修改动态文章管理
     */
    @PreAuthorize("@ss.hasPermi('system:dynamic:edit')")
    @Log(title = "动态文章管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Dynamic dynamic)
    {
        return toAjax(dynamicService.updateDynamic(dynamic));
    }

    /**
     * 删除动态文章管理
     */
    @PreAuthorize("@ss.hasPermi('system:dynamic:remove')")
    @Log(title = "动态文章管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{dynamicIds}")
    public AjaxResult remove(@PathVariable Long[] dynamicIds)
    {
        return toAjax(dynamicService.deleteDynamicByDynamicIds(dynamicIds));
    }
}
